// 节点属性定义接口
export interface NodePropertyConfig {
    label: string;
    type: 'string' | 'number' | 'boolean' | 'select' | 'code' | 'color' | 'date';
    default?: any;
    options?: string[]; // 用于select类型
    language?: string;  // 用于code类型
    required?: boolean;
    description?: string;
}

// 节点端口定义接口
export interface NodePortConfig {
    id: string;
    name: string;
    type: string;
}

// 节点类型配置接口
export interface NodeTypeConfig {
    label: string;
    icon: string;
    color: string;
    description?: string;
    category?: string;
    shortLabel?: string;
    properties?: Record<string, NodePropertyConfig>;
    ports?: {
        inputs: NodePortConfig[];
        outputs: NodePortConfig[];
    };
    inputs?: any[]; // 兼容旧版本
    outputs?: any[]; // 兼容旧版本
}


// 节点类型配置映射
// 在 nodeTypes 对象中添加系统节点
export const nodeTypes: Record<string, NodeTypeConfig> = {
    // 系统节点
    start: {
        label: '开始节点',
        shortLabel: '开始',
        icon: 'CircleCheck',
        color: '#67C23A',
        category: '系统',
        description: '工作流的起始节点',
        properties: {
            description: {
                label: '描述',
                type: 'string',
                description: '节点描述信息'
            },
            startTrigger: {
                label: '触发方式',
                type: 'select',
                options: ['手动触发', '定时触发', '事件触发'],
                default: '手动触发',
                description: '工作流启动的触发方式'
            }
        },
        ports: {
            inputs: [],
            outputs: [{id: 'output', name: '输出', type: 'flow'}]
        },
        inputs: [],
        outputs: [
            {id: 'output', name: '输出'}
        ]
    },

    end: {
        label: '结束节点',
        shortLabel: '结束',
        icon: 'CircleClose',
        color: '#F56C6C',
        category: '系统',
        description: '工作流的结束节点',
        properties: {
            description: {
                label: '描述',
                type: 'string',
                description: '节点描述信息'
            },
            exitCode: {
                label: '退出代码',
                type: 'number',
                default: 0,
                description: '工作流退出代码，0表示成功'
            }
        },
        ports: {
            inputs: [{id: 'input', name: '输入', type: 'flow'}],
            outputs: []
        },
        inputs: [
            {id: 'input', name: '输入'}
        ],
        outputs: []
    },

    condition: {
        label: '条件节点',
        shortLabel: '条件',
        icon: 'Opportunity',
        color: '#E6A23C',
        category: '系统',
        description: '根据条件选择不同的执行路径',
        properties: {
            condition: {
                label: '条件表达式',
                type: 'string',
                description: '条件判断表达式'
            }
        },
        ports: {
            inputs: [{id: 'input', name: '输入', type: 'flow'}],
            outputs: [
                {id: 'true', name: '满足条件', type: 'flow'},
                {id: 'false', name: '不满足条件', type: 'flow'}
            ]
        },
        inputs: [
            {id: 'input', name: '输入'}
        ],
        outputs: [
            {id: 'true', name: '是'},
            {id: 'false', name: '否'}
        ]
    },

    parallel: {
        label: '并行节点',
        shortLabel: '并行',
        icon: 'Connection',
        color: '#409EFF',
        category: '系统',
        description: '并行执行多个分支',
        properties: {
            branches: {
                label: '分支数量',
                type: 'number',
                default: 2,
                description: '并行执行的分支数量'
            }
        },
        ports: {
            inputs: [{id: 'input', name: '输入', type: 'flow'}],
            outputs: [
                {id: 'branch1', name: '分支1', type: 'flow'},
                {id: 'branch2', name: '分支2', type: 'flow'}
            ]
        },
        inputs: [
            {id: 'input', name: '输入'}
        ],
        outputs: [
            {id: 'output1', name: '输出1'},
            {id: 'output2', name: '输出2'}
        ]
    },

    delay: {
        label: '延时节点',
        shortLabel: '延时',
        icon: 'Timer',
        color: '#909399',
        category: '系统',
        description: '延迟执行后续节点',
        properties: {
            delayTime: {
                label: '延迟时间(秒)',
                type: 'number',
                default: 5,
                description: '延迟执行的时间(秒)'
            }
        },
        ports: {
            inputs: [{id: 'input', name: '输入', type: 'flow'}],
            outputs: [{id: 'output', name: '输出', type: 'flow'}]
        },
        inputs: [
            {id: 'input', name: '输入'}
        ],
        outputs: [
            {id: 'output', name: '输出'}
        ]
    },

    // 数据节点（原有节点）
    dataSource: {
        label: '数据源',
        icon: 'DataLine',
        color: '#67C23A',
        category: '输入',
        description: '从外部数据源获取数据',
        properties: {
            connectionString: {
                label: '连接字符串',
                type: 'string',
                required: true,
                description: '数据源的连接字符串或URL'
            },
            refreshInterval: {
                label: '刷新间隔(秒)',
                type: 'number',
                default: 60,
                description: '数据自动刷新的时间间隔'
            },
            dataFormat: {
                label: '数据格式',
                type: 'select',
                options: ['JSON', 'CSV', 'XML', 'SQL'],
                default: 'JSON',
                description: '数据源提供的数据格式'
            }
        },
        ports: {
            inputs: [],
            outputs: [{id: 'output', name: '输出', type: 'dataset'}]
        }
    },

    filter: {
        label: '数据过滤',
        icon: 'Filter',
        color: '#409EFF',
        category: '处理',
        description: '根据条件过滤数据',
        properties: {
            condition: {
                label: '过滤条件',
                type: 'string',
                description: '过滤数据的条件表达式'
            },
            caseSensitive: {
                label: '区分大小写',
                type: 'boolean',
                default: false,
                description: '字符串比较是否区分大小写'
            },
            keepMatched: {
                label: '保留匹配项',
                type: 'boolean',
                default: true,
                description: '是否保留匹配条件的数据（否则保留不匹配的）'
            }
        },
        ports: {
            inputs: [{id: 'input', name: '输入', type: 'dataset'}],
            outputs: [{id: 'output', name: '输出', type: 'dataset'}]
        }
    },

    transform: {
        label: '数据转换',
        icon: 'Magic',
        color: '#E6A23C',
        category: '处理',
        description: '转换数据格式或结构',
        properties: {
            transformScript: {
                label: '转换脚本',
                type: 'code',
                language: 'javascript',
                description: '用于转换数据的JavaScript代码'
            },
            outputSchema: {
                label: '输出模式',
                type: 'string',
                description: '输出数据的结构描述（可选）'
            }
        },
        ports: {
            inputs: [{id: 'input', name: '输入', type: 'dataset'}],
            outputs: [{id: 'output', name: '输出', type: 'dataset'}]
        }
    },

    getCurrentTime: {
        label: '获取当前时间',
        icon: 'Time',
        color: '#F56C6C',
        category: '系统',
        description: '获取当前时间',
        properties: {
            format: {
                label: '时间格式',
                type: 'string',
                description: '时间格式'
            },
            timeZone: {
                label: '时区',
                type: 'select',
                options: ['UTC', 'Asia/Shanghai'],
                default: 'UTC',
                description: '时区'
            },
            locale: {
                label: '语言',
                type: 'string',
                description: '语言'
            }
        },
        ports: {
            inputs: [{id: 'input', name: '输入', type: 'dataset'}],
            outputs: [{id: 'output', name: '输出', type: 'dataset'}]
        }
    },

    aggregate: {
        label: '数据聚合',
        icon: 'Histogram',
        color: '#9254DE',
        category: '处理',
        description: '对数据进行分组和聚合操作',
        properties: {
            groupByFields: {
                label: '分组字段',
                type: 'string',
                description: '用于分组的字段，多个字段用逗号分隔'
            },
            aggregations: {
                label: '聚合操作',
                type: 'string',
                description: '聚合操作定义，如：sum(value),avg(price)'
            }
        },
        ports: {
            inputs: [{id: 'input', name: '输入', type: 'dataset'}],
            outputs: [{id: 'output', name: '输出', type: 'dataset'}]
        }
    },

    join: {
        label: '数据连接',
        icon: 'Connection',
        color: '#F56C6C',
        category: '处理',
        description: '连接两个数据源',
        properties: {
            joinType: {
                label: '连接类型',
                type: 'select',
                options: ['内连接', '左连接', '右连接', '全连接'],
                default: '内连接',
                description: '数据连接的类型'
            },
            leftKey: {
                label: '左侧键',
                type: 'string',
                description: '左侧数据源的连接键'
            },
            rightKey: {
                label: '右侧键',
                type: 'string',
                description: '右侧数据源的连接键'
            }
        },
        ports: {
            inputs: [
                {id: 'left', name: '左输入', type: 'dataset'},
                {id: 'right', name: '右输入', type: 'dataset'}
            ],
            outputs: [{id: 'output', name: '输出', type: 'dataset'}]
        }
    },

    visualization: {
        label: '数据可视化',
        icon: 'PieChart',
        color: '#F56C6C',
        category: '输出',
        description: '将数据可视化为图表',
        properties: {
            chartType: {
                label: '图表类型',
                type: 'select',
                options: ['柱状图', '折线图', '饼图', '散点图', '雷达图', '热力图'],
                default: '柱状图',
                description: '可视化图表的类型'
            },
            title: {
                label: '标题',
                type: 'string',
                description: '图表的标题'
            },
            xField: {
                label: 'X轴字段',
                type: 'string',
                description: 'X轴对应的数据字段'
            },
            yField: {
                label: 'Y轴字段',
                type: 'string',
                description: 'Y轴对应的数据字段'
            },
            colorField: {
                label: '颜色字段',
                type: 'string',
                description: '用于区分数据系列的字段'
            },
            theme: {
                label: '主题',
                type: 'select',
                options: ['默认', '暗黑', '科技蓝', '商务'],
                default: '默认',
                description: '图表的视觉主题'
            }
        },
        ports: {
            inputs: [{id: 'input', name: '输入', type: 'dataset'}],
            outputs: []
        }
    },

    output: {
        label: '数据输出',
        icon: 'Upload',
        color: '#909399',
        category: '输出',
        description: '将数据输出到外部系统',
        properties: {
            outputType: {
                label: '输出类型',
                type: 'select',
                options: ['文件', 'API', '数据库'],
                default: '文件',
                description: '数据输出的目标类型'
            },
            destination: {
                label: '目标位置',
                type: 'string',
                description: '输出目标的路径或URL'
            },
            format: {
                label: '输出格式',
                type: 'select',
                options: ['JSON', 'CSV', 'Excel', 'SQL'],
                default: 'JSON',
                description: '输出数据的格式'
            },
            overwrite: {
                label: '覆盖已有',
                type: 'boolean',
                default: false,
                description: '是否覆盖已存在的目标'
            }
        },
        ports: {
            inputs: [{id: 'input', name: '输入', type: 'dataset'}],
            outputs: []
        }
    },

    conditional: {
        label: '条件分支',
        icon: 'SwitchButton',
        color: '#FF9800',
        category: '流程控制',
        description: '根据条件将数据分流',
        properties: {
            condition: {
                label: '条件表达式',
                type: 'string',
                description: '判断条件的表达式'
            }
        },
        ports: {
            inputs: [{id: 'input', name: '输入', type: 'dataset'}],
            outputs: [
                {id: 'true', name: '满足条件', type: 'dataset'},
                {id: 'false', name: '不满足条件', type: 'dataset'}
            ]
        }
    },

    custom: {
        label: '自定义节点',
        icon: 'Edit',
        color: '#8E44AD',
        category: '高级',
        description: '自定义处理逻辑的节点',
        properties: {
            script: {
                label: '处理脚本',
                type: 'code',
                language: 'javascript',
                description: '自定义数据处理的JavaScript代码'
            },
            inputPorts: {
                label: '输入端口数',
                type: 'number',
                default: 1,
                description: '节点的输入端口数量'
            },
            outputPorts: {
                label: '输出端口数',
                type: 'number',
                default: 1,
                description: '节点的输出端口数量'
            }
        },
        ports: {
            inputs: [{id: 'input', name: '输入', type: 'dataset'}],
            outputs: [{id: 'output', name: '输出', type: 'dataset'}]
        }
    }
};

/**
 * 获取节点类型配置
 * @param type 节点类型
 * @returns 节点类型配置
 */
export function getNodeTypeConfig(type: string): NodeTypeConfig | null {
    return nodeTypes[type] || null;
}

/**
 * 获取所有节点类型
 * @returns 所有节点类型的配置
 */
export function getAllNodeTypes(): Record<string, NodeTypeConfig> {
    return nodeTypes;
}

/**
 * 按类别获取节点类型
 * @returns 按类别分组的节点类型
 */
export function getNodeTypesByCategory(): Record<string, Record<string, NodeTypeConfig>> {
    const result: Record<string, Record<string, NodeTypeConfig>> = {};

    Object.entries(nodeTypes).forEach(([type, config]) => {
        const category = config.category || '其他';
        if (!result[category]) {
            result[category] = {};
        }
        result[category][type] = config;
    });

    return result;
}